package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.LocalAttrHelper;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.LocalIPV4ChangeTypeAttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

/**
 * <Description> <br>
 *
 * @author cheng.leizhen01<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021年3月30日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler <br>
 */
@Service
@AttrHandler(LocalIPV4ChangeTypeAttrHandler.class)
public class LocalIPV4ChangeTypeAttrLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData data) {
        if (Objects.isNull(attr)) {
            return;
        }

        //IP地址来源
        KiteAttr IPAddressSource = inst.getAttr("2_0000060");

        //IP地址变更
        KiteAttr IPProtocoVer = inst.getAttr("2_0000047");   //IP协议版本

        // IPV4增加地址数量
        KiteAttr IPV4AddNumber = inst.getAttr("210004180");
        //具体增加的IPV4地址
        KiteAttr IPV4Address = inst.getAttr("210004186");

        Map<String, Object> loginExtParams = ContextUtil.getLoginInfo().getUserInfo().getExtParams();
        String sceneInitParam = KiteMapUtils.getStrValue(loginExtParams, "upServiceOfferId");
        IQuerySceneInstService querySceneInstService = SpringUtil.getBean(IQuerySceneInstService.class);
        String upServiceOfferId = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "upServiceOfferId");
        boolean modifyChaseOrder = LocalServiceOfferConsts.SERVICE_OFFER_FOLLOW.equals(data.getServiceOfferId())
            && (KiteStringUtils.isNotEmpty(sceneInitParam) || KiteStringUtils.isNotEmpty(upServiceOfferId));

        //联通分配1_0000258
        if(KiteStringUtils.equals(data.getServiceOfferId(),"2826" ) || modifyChaseOrder){
            if(KiteStringUtils.equalsAny(IPAddressSource.getValue(),"1_0000258")){
                if("1_0000219".equals(IPProtocoVer.getValue())){
                    LocalAttrHelper.showAndNotNull(inst, "210004178");
                }
                if("1_0000220".equals(IPProtocoVer.getValue())){
                    LocalAttrHelper.showAndNotNull(inst, "210004179");
                }
                if("1_0000221".equals(IPProtocoVer.getValue())){
                    LocalAttrHelper.showAndNotNull(inst, "210004179","210004178");
                }
                //增加
                if(KiteStringUtils.equalsAny(attr.getValue(),"110007223")){
                    LocalAttrHelper.showAndNotNull(inst, "210004180");
                    LocalAttrHelper.hideAndNullable(inst, "210004182","210004184","210004186");
                } else if (KiteStringUtils.equalsAny(attr.getValue(),"110007224")){
                    LocalAttrHelper.showAndNotNull(inst, "210004182","210004184");
                    LocalAttrHelper.hideAndNullable(inst, "210004180","210004186");
                } else {
                    LocalAttrHelper.hideAndNullable(inst, "210004180","210004182","210004184","210004186");
                }
            } else {
                if(KiteStringUtils.equalsAny(attr.getValue(),"110007223")){
                    LocalAttrHelper.showAndNotNull(inst, "210004180","210004186");
                    LocalAttrHelper.hideAndNullable(inst, "210004182","210004184");
                }else if (KiteStringUtils.equalsAny(attr.getValue(),"110007224")){
                    LocalAttrHelper.showAndNotNull(inst, "210004182","210004184");
                    LocalAttrHelper.hideAndNullable(inst, "210004180","210004186");
                }else {
                    LocalAttrHelper.hideAndNullable(inst, "210004180","210004182","210004184","210004186");
                }
            }
        }

    }

    @Override
    public void handleStyle(KiteAttr attr, KiteInst inst, AttrData data) {

    }

}
